import odoo from '@/odoo'

const model = 'res.partner'
const Model = odoo.env(model);

const domain = [['id','!=',1],['is_company','=',true]]

export default {
  namespace: 'partnerOrgModel',
  state: {
    ids: [],
    recordsList: [],
    visible: {
      newForm: false,
      editForm: false,
    },
    record: {},
  },

  effects: {
    *listOrg(_, { call, put }) {
      const records = yield Model.search_org(domain)
      const recordsList = records.look2()
      const ids = recordsList.map(item=>item.id)

      yield put({
        type: 'save',
        payload: {ids, recordsList},
      });
    },

    *listPerson(_, { call, put }) {
      const records = yield Model.search_person(domain)
      const recordsList = records.look2()
      const ids = recordsList.map(item=>item.id)

      yield put({
        type: 'save',
        payload: {ids, recordsList},
      });
    },

    *create({payload}, { call, put }) {
      const { vals } = payload
      //const vals2 = {...vals, is_company: true}
      const record_ins = yield Model.create(vals)
      if (record_ins.id){
        const record = record_ins.look()
        yield put({
          type: 'add',
          payload: {id: record_ins.id, record },
        });
      }
    },

    *write({payload}, { call, put }) {
      const { id, vals } = payload
      const vals2 = {...vals }
      const record_ins = yield Model.write(id, vals2)
      if (record_ins.id){
        const record = record_ins.look()
        yield put({
          type: 'save',
          payload: {id: record_ins.id, record },
        });
      }
    },

    *create_child({payload}, { call, put }) {
      const { id, vals } = payload
      const vals2 = {...vals }
      const record_ins = yield Model.write(id, vals2)
      if (record_ins.id){
        const record = record_ins.look()
        yield put({
          type: 'save',
          payload: {id: record_ins.id, record },
        });
      }
    },


  },

  reducers: {
    toggleVisible(state, { payload }) {
      const { visible } = state
      return {
        ...state,
        visible: { ...visible, ...payload } ,
      };
    },

    show(state, { payload }) {
      const { visible } = state
      const nv = {}
      nv[payload] = true

      return {
        ...state,
        visible: { ...visible, ...nv } ,
      };
    },

    hide(state, { payload }) {
      const { visible } = state
      const nv = {}
      nv[payload] = false

      return {
        ...state,
        visible: { ...visible, ...nv } ,
      };
    },

    add(state, { payload }) {
      const { id, record } = payload
      const { ids, recordsList } = state

      return {
        ...state,
        ids: [...ids, id ],
        recordsList: [...recordsList, record ],
      };
    },

    view(state, { payload }) {
      const {id} = payload
      const rec = Model.view(id)

      return {
        ...state,
        record: rec.look()
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },


  },
};
